<?php
//checks the first link in each post and tries to save an image source to postmeta
//Works with V1-1 and earlier savelink saved pages

include_once ("wp-load.php");
$args = array( 'post_type' => 'post', 'numberposts' => -1, 'post_status' => 'publish');

$ids = get_posts($args);

foreach ($ids as $id){
    $pst = get_post($id, ARRAY_A);
    $content = $pst['post_content'];
    $pos = preg_match('{savelink/([^"|\\\\"]*)"|\\\\"}',$content,$matches);  ///([^/]+)/?$
    if ($pos){
        $filename = $matches[1];
        $directoryName = MCT_SL_SAVEPAGES;
        $extension = ".html";
        $full_url = site_url().'/'.$directoryName.'/'.$filename.$extension;
        $page = file_get_contents($full_url);
        $regexp1 = '{<img [^>]*src\s*=\s*("|\')([^"\']*)("|\')[^>]*>}i';  //used .\' because couldn't end up with \' 
        $pos = preg_match($regexp1,$page,$matches);
        if ($pos){
                $image = $matches[2];
                update_post_meta($pst['ID'], 'mct_sl_image',$image);
        }
    }
}
    
echo $a;
?>